private void NetClientProc(NetConnData conn) { conn.isActive = true; int bytesReceived = 0; byte[] bufferTmp = new byte[tmpBufferSize]; try { while (conn.client.Connected && conn.isActive) { if (conn.readyToSend && conn.stream.CanWrite && conn.messageQueue.Count > 0) { byte[] sendBuffer; lock (conn.messageQueue) { sendBuffer = conn.messageQueue.Dequeue().WrapMessage(); conn.readyToSend = false; } conn.stream.BeginWrite(sendBuffer, 0, sendBuffer.Length, new System.AsyncCallback(MessageSentToClient), conn); } bytesReceived = 0; while (conn.stream.DataAvailable) { conn.messageReceiveTime = (ulong)System.DateTime.Now.Ticks; bytesReceived = conn.stream.Read(bufferTmp, 0, bufferTmp.Length); ProcessReceivedData(bufferTmp, bytesReceived, conn); } Thread.Sleep(0); } } catch (System.Exception ex) { //Debug.LogException(ex); LogErrorToConsole(ex); } conn.isActive = false; conn.messageQueue.Clear(); //Debug.Log("Connection " + conn.ID + " to " + conn.remoteEP + " closed."); LogToConsole(serverName + " connection to client " + conn.remoteIP + " closed."); conn.stream.Close(); conn.client.Close(); if (connections.Contains(conn)) { connections.Remove(conn); } }
//public void CloseConnection(int connId) //{ // foreach (NetConnData conn in connections) // { // if (conn.ID == connId) // { // //Debug.Log(serverName + " closing connection " + conn.ID + " to " + conn.remoteEP); // LogToConsole(serverName + " closing connection to client " + conn.remoteIP); // conn.isActive = false; // connections.Remove(conn); // break; // } // } //} public void CloseConnectionsTo(string remoteIP) { for (int i = connections.Count - 1; i >= 0; i--) { NetConnData conn = connections[i]; if (conn.remoteIP == remoteIP) { //Debug.Log(serverName + " closing connection " + conn.ID + " to " + conn.remoteEP); LogToConsole(serverName + " closing connection to client " + conn.remoteIP); conn.isActive = false; connections.Remove(conn); } } }
private void ProcessReceivedData(byte[] buffer, int bytesReceived, NetConnData conn) { if (conn.bytesReceived == 0 && bytesReceived > 3) //new message { int newMessageSize = System.BitConverter.ToInt32(buffer, 0) + sizeof(int); //prefix is one int if (newMessageSize != conn.messageSize) { conn.messageSize = newMessageSize; conn.messageBuffer = new byte[newMessageSize]; } } int availableLength = conn.messageSize - conn.bytesReceived; int copyLen = Mathf.Min(availableLength, bytesReceived); conn.packetCounter++; System.Array.Copy(buffer, 0, conn.messageBuffer, conn.bytesReceived, copyLen); conn.bytesReceived += copyLen; if (conn.bytesReceived == conn.messageSize) { conn.ResetCounters(); NetMessageData message = new NetMessageData(); //message.SetDecompressor(decompressor); message.UnwrapMessage(conn.messageBuffer); ReceivedMessage?.Invoke(conn, new ReceivedMessageEventArgs(message)); } if (copyLen != bytesReceived) { byte[] newBuffer = new byte[bytesReceived - copyLen]; System.Array.Copy(buffer, copyLen, newBuffer, 0, bytesReceived - copyLen); ProcessReceivedData(newBuffer, bytesReceived - copyLen, conn); } }
private void MessageSentToClient(System.IAsyncResult ar) { NetConnData conn = (NetConnData)ar.AsyncState; try { conn.stream.EndWrite(ar); } catch (System.Exception ex) { if (conn.isActive) { //Debug.LogError(serverName + " error sending to client."); LogErrorToConsole(serverName + " error sending to client: " + ex.Message); //Debug.LogException(ex); conn.isActive = false; } } conn.readyToSend = true; }
private void HandleNetClientConnected(System.IAsyncResult ar) { TcpListener server = (TcpListener)ar.AsyncState; try { NetConnData conn = new NetConnData(); conn.client = server.EndAcceptTcpClient(ar); conn.stream = conn.client.GetStream(); conn.remoteIP = conn.client.Client.RemoteEndPoint.ToString(); int iP = conn.remoteIP.IndexOf(':'); if (iP >= 0) { conn.remoteIP = conn.remoteIP.Substring(0, iP); } connections.Add(conn); allDoneServer.Set(); //Debug.Log(serverName + " connected new client: " + conn.remoteEP); LogToConsole(serverName + " connected to client " + conn.remoteIP); NetClientProc(conn); } catch (System.ObjectDisposedException) { // do nothing } catch (System.Exception ex) { //Debug.LogError(serverName + " error while connecting: " + ex.Message); LogErrorToConsole(serverName + " error while connecting: " + ex.Message); //Debug.LogException(ex); } }