コード例 #1
0
ファイル: SocketSession.cs プロジェクト: coreystj/TurningEdge
 protected void SendCallback(IAsyncResult ar)
 {
     DoTry(() =>
     {
         // Retrieve the socket from the state object.
         SocketSession session = (SocketSession)ar.AsyncState;
         int bytesSent         = session.CurrentSocket.EndSend(ar);
         if (OutGoingPackets.Count == 0)
         {
             FireOnMessageSentSuccess(this);
         }
     });
 }
コード例 #2
0
ファイル: SocketSession.cs プロジェクト: coreystj/TurningEdge
        private void ConnectCallback(IAsyncResult ar)
        {
            DoTry(() =>
            {
                // Retrieve the socket from the state object.
                SocketSession session = (SocketSession)ar.AsyncState;

                // Complete the connection.
                session.CurrentSocket.EndConnect(ar);

                session.CurrentSocket.BeginReceive(session.InBuffer, 0, Session.BUFFER_SIZE, 0,
                                                   new AsyncCallback(ReadCallback), session);

                FireOnConnected(session);
            });
        }
コード例 #3
0
ファイル: SocketSession.cs プロジェクト: coreystj/TurningEdge
        public void ReadCallback(IAsyncResult ar)
        {
            DoTry(() =>
            {
                // Retrieve the state object and the handler socket
                // from the asynchronous state object.
                SocketSession session = (SocketSession)ar.AsyncState;

                // Read data from the client socket.
                int bytesRead  = session.CurrentSocket.EndReceive(ar);
                byte[] inBytes = new byte[bytesRead];
                if (bytesRead > 0)
                {
                    Array.Copy(session.InBuffer, 0, inBytes, 0, bytesRead);
                    Packet packet = inBytes.ToPacket();

                    // Not all data received. Get more.
                    session.CurrentSocket.BeginReceive(
                        session.InBuffer, 0, Session.BUFFER_SIZE, 0,
                        new AsyncCallback(ReadCallback), session);

                    if (packet.Type != DataTypes.PacketType.None)
                    {
                        byte[] bytes = this.Append(packet);

                        if (bytes != null)
                        {
                            FireOnMessageReceivedSuccess(session, bytes);
                            if (_messages.Count > 0)
                            {
                                ProcessSend(_messages.Dequeue());
                            }
                        }
                        else
                        {
                            session.Send(new Packet(
                                             DataTypes.PacketType.None, new byte[] { 0 }));
                        }
                    }
                    else
                    {
                        session.Send(session.PopPacket());
                    }
                }
            });
        }
コード例 #4
0
ファイル: SocketSession.cs プロジェクト: coreystj/TurningEdge
        public void AcceptCallback(IAsyncResult ar)
        {
            DoTry(() =>
            {
                // Get the socket that handles the client request.
                SocketSession session = (SocketSession)ar.AsyncState;

                Socket clientSocket = session.CurrentSocket.EndAccept(ar);

                // Create the state object.
                Session clientSession = new SocketSession(clientSocket, _ipAddress, _port);

                clientSocket.BeginReceive(clientSession.InBuffer, 0, Session.BUFFER_SIZE, 0,
                                          new AsyncCallback(ReadCallback), clientSession);
                FireOnConnected(session);
            });

            Listen();
        }