示例#1
0
        private void CreateConnection(Socket socket)
        {
            try
            {
                Connection conn  = new Connection(socket, this);
                int        index = Helpers.GetFirstIndexFromList(ConnectionList);
                conn.cIndex = index;
                ConnectionList.Add(index, conn);
                conn.StartReceiveData();
                InvokeUI.UpdateConnectionsCount(ConnectionList.Count);

                if (WriteLogs)
                {
                    Logs.WriteLog("green", "Created connection for IP [{0}]", conn.IpAddress);
                }

                if (SendHello)
                {
                    ProtocolCore.SendHelloMessage(index);
                }
            }
            catch (Exception Ex)
            {
                if (WriteDebugLogs)
                {
                    Logs.WriteLog("red", Ex.Message);
                }
            }
        }
示例#2
0
        private void ReceiveData(IAsyncResult ar)
        {
            if (IsAlive)
            {
                try
                {
                    UdpClient socket = (UdpClient)ar.AsyncState;

                    byte[] data = socket.EndReceive(ar, ref serverEndPoint);

                    ProtocolCore.ProcessUDPPacket(data);

                    if (WriteDebugLogs)
                    {
                        Logs.WriteLog("black", "Recived Data : [{0}]", Helpers.ByteArrayToString(data));
                    }

                    udpServer.BeginReceive(new AsyncCallback(ReceiveData), socket);
                }
                catch (Exception Ex)
                {
                    if (WriteDebugLogs)
                    {
                        Logs.WriteLog("red", Ex.Message);
                    }
                }
            }
        }
        private void ReceiveData(IAsyncResult ar)
        {
            if (ConnectionSocket.Connected)
            {
                try
                {
                    if (ConnectionSocket.EndReceive(ar) > 0)
                    {
                        DataBuffer = Helpers.TrimNullByteData(DataBuffer);

                        if (ParentServer.WriteDebugLogs)
                        {
                            Logs.WriteLog("black", "Recived Data From Index [{0}] IP [{1}] Data : [{2}]", cIndex, IpAddress, Helpers.ByteArrayToString(DataBuffer));
                        }

                        ProtocolCore.ProcessCSPacket(this, DataBuffer);

                        Array.Clear(DataBuffer, 0, DataBuffer.Length);
                    }

                    ConnectionSocket.BeginReceive(DataBuffer, 0, DataBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), ConnectionSocket);
                }
                catch (Exception Ex)
                {
                    if (ParentServer.WriteDebugLogs)
                    {
                        Logs.WriteLog("red", "Lost Connection with index [{0}]. Closing Connection..", cIndex);
                        Logs.WriteLog("red", Ex.Message);
                    }

                    ParentServer.ConnectionList.Remove(cIndex);
                    this.Close();
                }
            }
        }