コード例 #1
0
 public bool Send(byte[] message, xConnection conn)
 {
     if (conn != null && conn.socket.Connected)
     {
         lock (conn.socket) {
             //we use a blocking mode send, no async on the outgoing
             //since this is primarily a multithreaded application, shouldn't cause problems to send in blocking mode
             conn.socket.Send(message, message.Length, SocketFlags.None);
         }
     }
     else
     {
         return(false);
     }
     return(true);
 }
コード例 #2
0
        private void acceptCallback(IAsyncResult result)
        {
            xConnection conn = null;             // new xConnection();

            try {
                //Finish accepting the connection
                System.Net.Sockets.Socket s = (System.Net.Sockets.Socket)result.AsyncState;
                conn        = new xConnection();
                conn.socket = s.EndAccept(result);
                conn.buffer = new byte[_bufferSize];
                conn.server = this;
                lock (_sockets) {
                    _sockets.Add(conn);
                }
                //Queue recieving of data from the connection
                conn.socket.BeginReceive(conn.buffer, 0, conn.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), conn);
                //Queue the accept of the next incomming connection
                _serverSocket.BeginAccept(new AsyncCallback(acceptCallback), _serverSocket);
            } catch (SocketException) {
                if (conn.socket != null)
                {
                    conn.socket.Close();
                    lock (_sockets) {
                        _sockets.Remove(conn);
                    }
                }
                //Queue the next accept, think this should be here, stop attacks based on killing the waiting listeners
                _serverSocket.BeginAccept(new AsyncCallback(acceptCallback), _serverSocket);
            } catch (Exception) {
                if (conn.socket != null)
                {
                    conn.socket.Close();
                    lock (_sockets) {
                        _sockets.Remove(conn);
                    }
                }
                //Queue the next accept, think this should be here, stop attacks based on killing the waiting listeners
                _serverSocket.BeginAccept(new AsyncCallback(acceptCallback), _serverSocket);
            }
        }
コード例 #3
0
        private void ReceiveCallback(IAsyncResult result)
        {
            //get our connection from the callback
            xConnection conn = (xConnection)result.AsyncState;

            //catch any errors, we'd better not have any
            try {
                //Grab our buffer and count the number of bytes receives
                int bytesRead = conn.socket.EndReceive(result);
                //make sure we've read something, if we haven't it supposadly means that the client disconnected
                if (bytesRead > 0)
                {
                    //put whatever you want to do when you receive data here
                    conn.ProcessBuffer(bytesRead);
                    conn.socket.BeginReceive(conn.buffer, 0, 4, SocketFlags.None, new AsyncCallback(ReceiveCallback), conn);
                }
                else
                {
                    //Callback run but no data, close the connection
                    //supposadly means a disconnect
                    //and we still have to close the socket, even though we throw the event later
                    conn.socket.Close();
                    lock (_sockets) {
                        _sockets.Remove(conn);
                    }
                }
            } catch (SocketException) {
                //Something went terribly wrong
                //which shouldn't have happened
                if (conn.socket != null)
                {
                    conn.socket.Close();
                    lock (_sockets) {
                        _sockets.Remove(conn);
                    }
                }
            }
        }
コード例 #4
0
ファイル: xSend.cs プロジェクト: polytronicgr/GalaxyE
 public xSend(xConnection conn)
 {
     this.conn = conn;
 }