/
net.bmx
167 lines (158 loc) · 4.2 KB
/
net.bmx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Const MESNEWMESSAGE:Double = 68740
'DO NOT USE 0 HERE
Const MES_CLICK:Short = 50
Const MES_SYNCBLOCKS:Short = 84
Const MES_RSEED:Short = 45
Const MES_NEWGAME:Short = 41
Const MES_CONNECT:Short = 1
Const MES_JOIN:Short = 2
Const MES_LEAVE:Short = 3
Const MES_JOINCONFIRM:Short = 4
Const MES_CHAT:Short = 5
Const MES_CURPLAYERS:Short = 6
Const MES_KICK:Short = 7
Const MES_PING:Short = 8
Const MES_SETCOLOR:Short = 9
Const MES_SETTYPE:Short = 100
Const MES_REFRESH:Short = 101
Const MES_SENDGAMES:Short = 102
'GLOBAL Net type. there only needs to be one
Type TNet
Global Bank:TBank = New TBank
Global Message:TBankStream = CreateBankStream(Bank)
Global Clients:TList = New TList
Global Clientsock:TSocket = TSocket.CreateTCP()
Global Serversock:TSocket = TSocket.CreateTCP()
Global NetStatus:Byte = 0 '0=singleplayer,1=hosting,2=joining
Global MaxPlayers:Byte = 8
Function Reset()
MClear()
DisconnecT()
End Function
Function MClear()
' If NetStatus = 0 Return
Message.Close()
Bank = New TBank
Message = CreateBankStream(bank)
End Function
Function MClientSendTCP(exclude:TNetClient = Null)
If netstatus = 0 Return
For Local i:TNetClient = EachIn clients
If Not (i = exclude)
MSend(i.tcpsock)
EndIf
Next
End Function
Function MNew(MessageType:Short)
'If NetStatus = 0 Return
Message.WriteDouble(MESNEWMESSAGE)
Message.WriteShort(MessageType)
End Function
Function MSend(sock:TSocket)
If NetStatus > 0 Or sock = TServerList.sock sock.Send(Message._bank._buf, Message._bank._size) Else Return
End Function
Function BSend(sock:TSocket, Buf:Byte Ptr, Count:Int)
'If NetStatus = 0 Return
sock.Send(Buf, Count)
End Function
Function MKickID(id:Byte)
MNew(MES_KICK)
MByte(id)
End Function
Function MByte(n:Byte)
'If NetStatus = 0 Return
Message.WriteByte(n)
End Function
Function MDouble(n:Double)
'If NetStatus = 0 Return
Message.WriteDouble(n)
End Function
Function MFloat(n:Float)
'If NetStatus = 0 Return
Message.WriteFloat(n:Float)
End Function
Function MInt(n:Int)
'If NetStatus = 0 Return
Message.WriteInt(n)
End Function
Function MLong(n:Long)
'If NetStatus = 0 Return
Message.WriteLong(n)
End Function
Function MShort(n:Short)
'If NetStatus = 0 Return
Message.WriteShort(n)
End Function
Function MLine(str:String)
'If NetStatus = 0 Return
Message.WriteLine(str)
End Function
Function MObject(obj:Object)
'If NetStatus = 0 Return
Message.WriteObject(obj)
End Function
Function MString(str:String)
'If NetStatus = 0 Return
Message.WriteString(str)
End Function
Function Recieve:TBankStream(sock:TSocket, length:Int) 'Reads length bytes from sock into a new bank and returns a bankstream
'If netstatus = 0 Return Null
Local bank:TBankStream = CreateBankStream(Null)
Local Buf:Byte[length]
sock.Recv(Buf, length)
bank.Write(Buf, length)
bank.Seek(0)
Return bank
End Function
Function Connect:Byte(cIP:Int = -1, cPort:Int = -1)
Clientsock.SetTCPNoDelay(1)
If Clientsock.Connect(cIP, cPORT)
Return 1
Else
Return Null
EndIf
End Function
Function Disconnect()
For Local c:TNetClient = EachIn clients
c.tcpsock.Close()
Next
Players.Clear()
SPlayers.Clear()
Clients = New TList
Clientsock.Close()
Clientsock = TSocket.CreateTCP()
Serversock.Close()
Serversock = TSocket.CreateTCP()
NetStatus = 0
ClearGadgetItems(gChatPList)
End Function
Function Host:Byte(cPort:Int = -1)
ServerSock.SetTCPNoDelay(1)
If Not Serversock.Bind(cPort)
Return 0
Else
serversock.Listen(0)
Connect(HostIp("localhost"), cPort)
TNet.NetStatus = 2
Return 1
EndIf
End Function
End Type
Type TNetClient
Field IP:Int, PORT:Int, tcpsock:TSocket
Function CreateClient:TNetClient(sock:TSocket)
' If TNet.NetStatus = 0 Return Null
Local n:TNetClient = New TNetClient
n.tcpsock = sock
n.IP = sock.RemoteIp()
n.PORT = sock.RemotePort()
Return n
EndFunction
Method New()
TNet.clients.AddLast(Self)
EndMethod
Method remove()
tcpsock.Close()
TNet.Clients.Remove(Self)
End Method
End Type