public void ReceiveMessageUDPCallback(IAsyncResult ar) { ReceiveMessageUDP(); try { byte[] data = udpClient.EndReceive(ar, ref remoteAddressUDP); onMessageRecieveUDP(NetData.RecoverBytes(data)); } catch { if (stream != null) { stream.Flush(); stream.Dispose(); stream = null; } if (tcpClient != null) { tcpClient.Client.Close(); tcpClient.Dispose(); tcpClient = null; } if (udpClient != null) { udpClient.Dispose(); udpClient = null; } onConnectionLost?.Invoke(); } }
public void Recieve(byte[] bytes) { NetData message = NetData.RecoverBytes(bytes); if (message != null && message.number > lastMessageNumber) { if (ServerHandler.clients.Contains(message.sender.ID)) { if ((ServerHandler.clients[message.sender.ID] as NetUser).udpConnection == null) { NetUser.OnUserConnectedUDP(message.sender); } netUser = ServerHandler.clients[message.sender.ID] as NetUser; netUser.udpConnection = this; } else { netUser = new NetUser(message.sender); netUser.udpConnection = this; ServerHandler.AddConnection(netUser); NetUser.OnUserConnectedUDP(message.sender); } NetUser.OnUdpMessageHandleCallback(bytes, message, netUser); lastMessageNumber = message.number; } }
internal static void InvokeClientAction(byte[] message, int bytesCount) { ushort type; NetData data = NetData.RecoverBytes(message, bytesCount, out type); if (data != null) { typesActions[type].Invoke(data.data, data.senderID); } }
public void GetMessage() { do { stream.Flush(); byte[] bytes = new byte[2048]; stream.Read(bytes, 0, bytes.Length); NetData message = NetData.RecoverBytes(bytes); if (message.number > lastMessageNumber) { if (ServerHandler.clients.Contains(message.sender.ID)) { if ((ServerHandler.clients[message.sender.ID] as NetUser).tcpConnection == null) { netUser = ServerHandler.clients[message.sender.ID] as NetUser; netUser.tcpConnection = this; NetUser.OnUserConnectedTCP(message.sender); } } else { netUser = new NetUser(message.sender); netUser.tcpConnection = this; ServerHandler.AddConnection(netUser); NetUser.OnUserConnectedTCP(message.sender); } if (netUser.info == null) { netUser.info = NetUser.server.GetUserData(message.sender.ID); } if (netUser.info == null) { netUser.info = ServerHandler.createUserBD(message.sender); } NetUser.OnTcpMessageHandleCallback(bytes, message, netUser); lastMessageNumber = message.number; } }while (stream.DataAvailable); }
public void ReceiveMessageTCPCallback(IAsyncResult ar) { ReceiveMessageTCP(); try { int bytesCount = stream.EndRead(ar); if (bytesCount > 0) { NetData message = NetData.RecoverBytes(ar.AsyncState as byte[]); onMessageRecieveTCP?.Invoke(message); } } catch (Exception e) { if (stream != null) { stream.Flush(); stream.Dispose(); stream = null; } if (tcpClient != null) { tcpClient.Client.Close(); tcpClient.Dispose(); tcpClient = null; } if (udpClient != null) { udpClient.Dispose(); udpClient = null; } onConnectionLost?.Invoke(); } }
public void GetMessage() { do { byte[] bytes = new byte[1024]; int bytesCount = 0; try { bytesCount = stream.Read(bytes, 0, bytes.Length); if (bytesCount < 2) { throw new Exception("null bytes"); } } catch { if (user != null) { ServerHandler.OnUserDisconnectedTCP(user); ServerHandler.OnUserDisconnectedUDP(user); ServerHandler.RemoveConnection(user); } Close(); return; } ushort type; bool transitable; NetData message = NetData.RecoverBytes(bytes, bytesCount, out type, out transitable); if (!transitable) { if (message != null) { if (ServerHandler.clients.ContainsKey(message.senderID)) { if (ServerHandler.clients[message.senderID].tcpConnection == null) { user = ServerHandler.clients[message.senderID]; user.tcpConnection = this; ServerHandler.OnUserConnectedTCP(user); } } else { user = new User(message.senderID); user.tcpConnection = this; ServerHandler.AddConnection(user); ServerHandler.OnUserConnectedTCP(user); Matchmaking.DefaultRoom.instance.JoinToRoom(user); if (user.info == null) { user.info = ServerHandler.DataBaseBehaivour.GetUserData(message.senderID); } if (user.info == null) { user.info = ServerHandler.DataBaseBehaivour.CreateUser(message.senderID); } } SynchronizableTypes.InvokeTypeActionTCP(type, bytes, message, ServerHandler.clients[message.senderID]); } else { if (user != null) { ServerHandler.OnUserDisconnectedTCP(user); ServerHandler.OnUserDisconnectedUDP(user); ServerHandler.RemoveConnection(user); } Close(); return; } } else { user.currentRoom.BroadcastMessageTCP(bytes); } }while (stream.DataAvailable); }
private static void GetMessage(UdpClient client, IAsyncResult ar) { IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 8001); UdpClient udpClient = (ar.AsyncState as UdpClient); byte[] bytes = null; try { bytes = udpClient.EndReceive(ar, ref iPEndPoint); if (bytes.Length < 2) { throw new Exception("null bytes"); } } catch { return; } finally { if (bytes != null) { ushort type; bool transitable; NetData message = NetData.RecoverBytes(bytes, bytes.Length, out type, out transitable); if (!transitable || !ServerHandler.udpConnections.ContainsKey(iPEndPoint)) { if (message != null) { User udpUser = null; if (ServerHandler.clients.ContainsKey(message.senderID)) { if (ServerHandler.clients[message.senderID].udpConnection == null) { ServerHandler.OnUserConnectedUDP(ServerHandler.clients[message.senderID]); UdpConnection udpConnection = new UdpConnection(iPEndPoint); udpConnection.user = ServerHandler.clients[message.senderID]; ServerHandler.clients[message.senderID].udpConnection = udpConnection; ServerHandler.udpConnections.Add(iPEndPoint, udpConnection); } udpUser = ServerHandler.clients[message.senderID]; } else { udpUser = new User(message.senderID); udpUser.udpConnection = new UdpConnection(iPEndPoint); ServerHandler.udpConnections.Add(iPEndPoint, udpUser.udpConnection); ServerHandler.AddConnection(udpUser); Matchmaking.DefaultRoom.instance.JoinToRoom(udpUser); ServerHandler.OnUserConnectedUDP(udpUser); if (udpUser.info == null) { udpUser.info = ServerHandler.DataBaseBehaivour.GetUserData(message.senderID); } if (udpUser.info == null) { udpUser.info = ServerHandler.DataBaseBehaivour.CreateUser(message.senderID); } } SynchronizableTypes.InvokeTypeActionUDP(type, bytes, message, ServerHandler.clients[message.senderID]); } } else { ServerHandler.udpConnections[iPEndPoint].user.currentRoom.BroadcastMessageUDP(bytes); } } } }