コード例 #1
0
ファイル: ConnectionManager.cs プロジェクト: LowerCode/tools
        public void SendWebSocketRequest(int websocketVersion, string AffinityCookie)
        {
            HandShakeRequest = Frames.GetHandShakeFrameWithAffinityCookie(Address.AbsoluteUri, websocketVersion, AffinityCookie);

            byte[] outputData = null;
            int    offset     = 0;

            while (offset < HandShakeRequest.Length)
            {
                outputData = HandShakeRequest[offset++];

                Client.Stream.BeginWrite(outputData, 0, outputData.Length, WriteCallback, Client);
                WebSocketUtil.LogVerbose("Client {0:D3}: Write {1} bytes: {2} ", Client.Id, outputData.Length,
                                         Encoding.UTF8.GetString(outputData, 0, outputData.Length));
            }
        }
コード例 #2
0
ファイル: ConnectionManager.cs プロジェクト: LowerCode/tools
        public Frame Send(byte[] outputData)
        {
            var frame = new Frame(outputData);

            ProcessSentData(frame);
            if (Client.TcpClient.Connected)
            {
                var result = Client.Stream.BeginWrite(outputData, 0, outputData.Length, WriteCallback, Client);
                WebSocketUtil.LogVerbose("Client {0:D3}: Write Type {1} : {2} ", Client.Id, frame.FrameType,
                                         frame.Content.Length);
            }
            else
            {
                Console.WriteLine("Connection is disconnected");
            }

            return(frame);
        }
コード例 #3
0
ファイル: ConnectionManager.cs プロジェクト: LowerCode/tools
        public void Initiate()
        {
            string host = Address.DnsSafeHost;
            int    port = Address.Port;

            Client = new Client();

            WebSocketUtil.LogVerbose("Connecting to {0} on {1}", host, port);

            Client.TcpClient = new MyTcpClient(host, port);
            Client.Stream    = Client.TcpClient.GetStream();
            IsAlwaysReading  = false;

            if (StoreData)
            {
                Client.DataSent     = new List <Frame>();
                Client.DataReceived = new List <Frame>();
            }
        }
コード例 #4
0
ファイル: ConnectionManager.cs プロジェクト: LowerCode/tools
        public Frame ReadData()
        {
            Frame frame = new Frame(new byte[] { });

            IAsyncResult result = Client.Stream.BeginRead(Client.InputData, 0, Client.InputData.Length, null, Client);

            if (result != null)
            {
                int bytesRead = Client.Stream.EndRead(result);
                if (bytesRead > 0)
                {
                    frame = new Frame(WebSocketUtil.SubArray(Client.InputData, 0, bytesRead));

                    ProcessReceivedData(frame);

                    WebSocketUtil.LogVerbose("Client {0:D3}: Read Type {1} : {2} ", Client.Id, frame.FrameType, frame.Content.Length);
                }
            }

            return(frame);
        }
コード例 #5
0
ファイル: ConnectionManager.cs プロジェクト: LowerCode/tools
        public void SendWebSocketRequest(int websocketVersion)
        {
            HandShakeRequest = Frames.GetHandShakeFrame(Address.AbsoluteUri, websocketVersion);

            byte[] outputData = null;
            int    offset     = 0;

            while (offset < HandShakeRequest.Length)
            {
                outputData = HandShakeRequest[offset++];

                var result = Client.Stream.BeginWrite(outputData, 0, outputData.Length, WriteCallback, Client);

                //jhkim debug
                //result.AsyncWaitHandle.WaitOne();

                WebSocketUtil.LogVerbose("Client {0:D3}: Write {1} bytes: {2} ", Client.Id, outputData.Length,
                                         Encoding.UTF8.GetString(outputData, 0, outputData.Length));

                //result.AsyncWaitHandle.Close();
            }
        }
コード例 #6
0
ファイル: ConnectionManager.cs プロジェクト: LowerCode/tools
        public void ReadDataCallback(IAsyncResult result)
        {
            Client client = (Client)result.AsyncState;

            if (client.IsDisposed)
            {
                return;
            }

            int       bytesRead        = client.Stream.EndRead(result); // wait until the buffer is filled
            int       bytesReadIntotal = bytesRead;
            ArrayList InputDataArray   = new ArrayList();

            byte[] tempBuffer = null;

            if (bytesRead > 0)
            {
                tempBuffer = WebSocketUtil.SubArray(Client.InputData, 0, bytesRead);

                // start looping if there is still remaining data
                if (client.TcpClient.GetStream().DataAvailable)
                {
                    // add the first buffer to the arrayList
                    InputDataArray.Add(tempBuffer);

                    // start looping appending to the arrayList
                    while (client.TcpClient.GetStream().DataAvailable)
                    {
                        bytesRead  = client.TcpClient.GetStream().Read(Client.InputData, 0, Client.InputData.Length);
                        tempBuffer = WebSocketUtil.SubArray(Client.InputData, 0, bytesRead);
                        InputDataArray.Add(tempBuffer);
                        bytesReadIntotal += bytesRead;
                        WebSocketUtil.LogVerbose("Looping: Client {0:D3}: bytesReadHere {1} ", Client.Id, bytesRead);
                    }

                    // create a single byte array with the arrayList
                    tempBuffer = new byte[bytesReadIntotal];
                    int arrayIndex = 0;
                    foreach (byte[] item in InputDataArray.ToArray())
                    {
                        for (int i = 0; i < item.Length; i++)
                        {
                            tempBuffer[arrayIndex] = item[i];
                            arrayIndex++;
                        }
                    }
                }

                // Create frame with the tempBuffer
                Frame frame = new Frame(tempBuffer);
                WebSocketUtil.LogVerbose("Client {0:D3}: Read Type {1} : {2} ", Client.Id, frame.FrameType, bytesReadIntotal);
                ProcessReceivedData(frame);

                // Send Pong if the frame was Ping
                if (frame.FrameType == FrameType.Ping)
                {
                    SendPong(frame);
                }

                if (client.IsDisposed)
                {
                    return;
                }

                // Start the Async Read to handle the next frame comming from server
                client.Stream.BeginRead(client.InputData, 0, client.InputData.Length, ReadDataCallback, client);
            }
            else
            {
                client.Dispose();
            }
        }