예제 #1
0
        protected override void ReceiveAsync(object sender, SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred > 0)
            {
                Buffer buffer = new Buffer(e.Buffer);
                buffer.Set(e.BytesTransferred);

                // new connection to server
                if (!EndPointIds.ContainsKey(e.RemoteEndPoint) && buffer.PeekByte() == Common.ICHI)
                {
                    int        connectionId = GetNextConnectionId();
                    Connection connection   = new Connection(this, connectionId, e.RemoteEndPoint, Socket);
                    Connections.Add(connectionId, connection);
                    EndPointIds.Add(e.RemoteEndPoint, connectionId);
                }

                if (EndPointIds.ContainsKey(e.RemoteEndPoint))
                {
                    Connections[EndPointIds[e.RemoteEndPoint]].OnReceive(buffer);
                }
            }

            Handle.Set();
        }
예제 #2
0
        public int Connect(string to, int port)
        {
            if (IPAddress.TryParse(to, out IPAddress iPAddress))
            {
                IPEndPoint endPoint     = new IPEndPoint(iPAddress, port);
                int        connectionId = GetNextConnectionId();
                Connection connection   = new Connection(this, connectionId, endPoint, Socket);
                Connections.Add(connectionId, connection);
                EndPointIds.Add(endPoint, connectionId);

                if (!IsReceiving)
                {
                    Thread.Start();
                }

                connection.InitializeHandshake();

                return(connectionId);
            }
            else
            {
                return(-1);
            }
        }