コード例 #1
0
        public ClientConnection(string id, int baseReceiveBufferSize, Socket socket, Action <ClientConnection> onConnected)
        {
            this.connectionId          = id;
            this.baseReceiveBufferSize = baseReceiveBufferSize;
            this.OnConnected           = onConnected;

            var endPoint = socket.LocalEndPoint;

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

            var sendArgs = new SocketAsyncEventArgs();

            sendArgs.AcceptSocket   = socket;
            sendArgs.RemoteEndPoint = endPoint;
            sendArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(OnSend);

            var receiveArgs = new SocketAsyncEventArgs();

            receiveArgs.AcceptSocket   = socket;
            receiveArgs.RemoteEndPoint = endPoint;
            receiveArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(OnReceived);

            socketToken = new ServerSocketToken(socket, baseReceiveBufferSize, sendArgs, receiveArgs);

            // handshake ready状態にセット
            socketToken.socketState = SocketState.WS_HANDSHAKING;

            // clientのhandshakeデータの受け取りを行う
            ReadyReceivingNewData(socketToken);
        }
コード例 #2
0
 private void ReadyReceivingNewData(ServerSocketToken token)
 {
     token.receiveArgs.SetBuffer(token.receiveBuffer, 0, token.receiveBuffer.Length);
     if (!token.socket.ReceiveAsync(token.receiveArgs))
     {
         OnReceived(token.socket, token.receiveArgs);
     }
 }
コード例 #3
0
        private void ReadBuffer(ServerSocketToken token)
        {
            var result = ScanBuffer(wsBuffer, wsBufLength);

            // read completed datas.
            if (0 < result.segments.Count)
            {
                OnMessage(result.segments);
            }

            // if the last result index is matched to whole length, receive finished.
            if (result.lastDataTail == wsBufLength)
            {
                wsBufIndex  = 0;
                wsBufLength = 0;
                ReadyReceivingNewData(token);
                return;
            }

            // unreadable data still exists in wsBuffer.
            var unreadDataLength = wsBufLength - result.lastDataTail;

            if (result.lastDataTail == 0)
            {
                // no data is read as WS data.
                // this means the all data in wsBuffer is not enough to read as WS data yet.
                // need more data to add the last of wsBuffer.

                // set wsBufferIndex and wsBufLength to the end of current buffer.
                wsBufIndex  = unreadDataLength;
                wsBufLength = unreadDataLength;
            }
            else
            {
                // not all of wsBuffer data is read as WS data.
                // data which is located before alreadyReadDataTail is already read.

                // move rest "unreaded" data to head of wsBuffer.
                Array.Copy(wsBuffer, result.lastDataTail, wsBuffer, 0, unreadDataLength);

                // then set wsBufIndex to
                wsBufIndex  = unreadDataLength;
                wsBufLength = unreadDataLength;
            }

            // should read rest.
            ReadyReceivingNewData(token);
        }