private void SendClientInfo(string name, Guid id) { ClientLoginMessage msg = new ClientLoginMessage(name, id); byte[] data = msg.ToBytes(); tcpSocket.BeginSend(data, 0, data.Length, 0, SendCallback, null); }
private void SocketReceiveCallback(IAsyncResult ar) { SocketStateObject state = (SocketStateObject)ar.AsyncState; try { Socket soc = state.Soc; int bytesIn = soc.EndReceive(ar); int pSize = BitConverter.ToInt32(state.Buff, 0); if (pSize == 0 | bytesIn == 0) { ISLogger.Write("Client {0} lost connection ", soc.RemoteEndPoint); socketList.Remove(soc); soc.Dispose(); state.TimeoutTimer.Dispose(); return; } int dRem = pSize; int bPos = 4; do { int bIn = state.Soc.Receive(state.Buff, bPos, dRem, 0); bPos += bIn; dRem = pSize - bPos + 4; } while (dRem > 0); state.TimeoutTimer.Dispose(); MessageType cmd = (MessageType)state.Buff[4]; if (cmd == MessageType.ClientLoginInfo) { //ISLogger.Write("{0} sent login info", soc.RemoteEndPoint); ClientLoginMessage msg = ClientLoginMessage.FromBytes(state.Buff); socketList.Remove(soc); ClientConnected?.Invoke(this, new ClientConnectedArgs(soc, msg.ClientName, msg.ClientGuid)); } else { ISLogger.Write("{0} sent invalid data", soc.RemoteEndPoint); } } catch (ObjectDisposedException) //This is fine, it just means that the socket was disposed by the timeout timer { //ISLogger.Write("ClientListener attempted to access disposed socket"); }catch (SocketException) { //TODO } }