Ejemplo n.º 1
0
        public bool SendAsync(Socket socket)
        {
            SocketBuffer socketBuffer = FindBuffer(socket);

            if (socketBuffer._SendBuffers.Count == 0)
            {
                return(true);
            }

            Byte[] btBuffer = socketBuffer._SendBuffers[0];

            socket.BeginSend(btBuffer, 0, btBuffer.Length, 0, new AsyncCallback(SendCallback), socket);
            return(true);
        }
Ejemplo n.º 2
0
        private SocketBuffer FindBuffer(Socket socket)
        {
            foreach (SocketBuffer socketBuffer in _bufferList)
            {
                if (socketBuffer._socket == socket)
                {
                    return(socketBuffer);
                }
            }

            SocketBuffer newBuffer = new SocketBuffer();

            newBuffer._socket = socket;
            _bufferList.Add(newBuffer);

            return(newBuffer);
        }
Ejemplo n.º 3
0
        private static void SendCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Socket handler = (Socket)ar.AsyncState;

                // Complete sending the data to the remote device.
                int bytesSent = handler.EndSend(ar);

                SocketBuffer socketBuffer = Server.GetInstance().FindBuffer(handler);

                socketBuffer._SendBuffers.RemoveAt(0);

                Server.GetInstance().SendAsync(handler);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Ejemplo n.º 4
0
        private void HandleEvent(BinaryReader br, Socket socket)
        {
            try
            {
                NotifyType notifyType = (NotifyType)DecodeInteger(br);

                // Temp
                //SetError(ErrorType.Fail_ReceiveSocket, notifyType.ToString());

                BaseInfo baseInfo = BaseInfo.CreateInstance(br);

                _NotifyHandler(notifyType, socket, baseInfo);

                SocketBuffer socketBuffer = FindBuffer(socket);
                socketBuffer._CompleteBuffer = null;
            }
            catch (Exception ex)
            {
                _errorLogs.Add(ex.ToString());
                //BaseInfo.SetError(ErrorType.Exception_Occur, ex.Message + "\n" + ex.StackTrace);
                //ErrorView.AddErrorString();
            }
        }
Ejemplo n.º 5
0
        public void OnDataReceived(IAsyncResult asyn)//For Server Mode
        {
            lock (_objLockMain)
            {
                KeyValuePair aKeyValuePair = (KeyValuePair)asyn.AsyncState;

                Socket socket = aKeyValuePair._Socket;
                Byte[] buffer = aKeyValuePair._DataBuffer;


                try
                {
                    int iRx = socket.EndReceive(asyn);
                    if (iRx == 0 && socket.Available == 0)
                    {
                        socket.Shutdown(SocketShutdown.Both);
                        socket.Disconnect(true);
                        socket.Close();
                        _NotifyHandler(NotifyType.Notify_Socket, socket, null);
                    }

                    MemoryStream ms = new MemoryStream(buffer, false);
                    BinaryReader br = new BinaryReader(ms);

                    // Temp
                    //SetError(ErrorType.Fail_ReceiveSocket, iRx.ToString());

                    SocketBuffer socketBuffer = FindBuffer(socket);

                    while (iRx > 0)
                    {
                        if (socketBuffer._CompleteBuffer == null)
                        {
                            HeaderInfo headerInfo = (HeaderInfo)BaseInfo.CreateInstance(br);

                            if (headerInfo == null)
                            {
                                break;
                            }

                            if (headerInfo._Header != HeaderInfo.Marker)
                            {
                                break;
                            }

                            socketBuffer._CompleteCount = headerInfo._BodySize;
                            socketBuffer._ReceiveCount  = iRx - headerInfo.GetSize();

                            if (socketBuffer._CompleteCount > socketBuffer._ReceiveCount)
                            {
                                socketBuffer._CompleteBuffer = new byte[socketBuffer._CompleteCount + 1];

                                for (int i = 0; i < socketBuffer._ReceiveCount; i++)
                                {
                                    socketBuffer._CompleteBuffer[i] = br.ReadByte();
                                }

                                break;
                            }

                            iRx -= headerInfo.GetSize();
                            iRx -= socketBuffer._CompleteCount;
                        }
                        else
                        {
                            int readCount = socketBuffer._CompleteCount - socketBuffer._ReceiveCount;

                            if (readCount > iRx)
                            {
                                readCount = iRx;
                            }

                            for (int i = 0; i < readCount; i++)
                            {
                                socketBuffer._CompleteBuffer[socketBuffer._ReceiveCount + i] = br.ReadByte();
                            }

                            socketBuffer._ReceiveCount += readCount;

                            if (socketBuffer._CompleteCount > socketBuffer._ReceiveCount)
                            {
                                break;
                            }

                            iRx -= readCount;
                        }

                        if (socketBuffer._CompleteBuffer != null)
                        {
                            MemoryStream memoryStream = new MemoryStream(socketBuffer._CompleteBuffer, false);
                            BinaryReader binaryReader = new BinaryReader(memoryStream);

                            HandleEvent(binaryReader, socket);

                            binaryReader.Close();
                            memoryStream.Close();
                        }
                        else
                        {
                            HandleEvent(br, socket);
                        }
                    }
                    br.Close();
                    ms.Close();

                    WaitForData(socket);

                    System.Threading.Thread.Sleep(0);
                }
                catch (Exception e)
                {
                    SetError(ErrorType.Fail_ReceiveSocket, "소켓수신 실패: 소켓을 열수 없습니다." + e.ToString());
                    //socket.Shutdown(SocketShutdown.Both);
                    //socket.Disconnect(true);
                    //socket.Close();
                    //_NotifyHandler(NotifyType.Notify_Socket, socket, null);
                }
            }
        }
Ejemplo n.º 6
0
        // 2013-12-27: GreenRose
        //public void SocketTimer(object obj, System.Timers.ElapsedEventArgs e)
        //{
        //    if (_listSocketInfo.Count > 0 && _bSendFinished)
        //    {
        //        SendSocketInfo(_listSocketInfo[0].socket, _listSocketInfo[0].notifyType, _listSocketInfo[0].baseInfo);
        //    }
        //}

        // 2013-12-27: GreenRose
        //private bool SendSocketInfo(Socket socket, NotifyType notifyType, BaseInfo sendInfo)
        //{
        //    _bSendFinished = false;

        //    try
        //    {
        //        if (socket.Connected == false)        // auto
        //            return true;

        //        int sendSize = 4 + sendInfo.GetSize();

        //        HeaderInfo headerInfo = new HeaderInfo();
        //        headerInfo._BodySize = sendSize;

        //        byte[] btBuffer = new byte[headerInfo.GetSize() + sendSize];

        //        MemoryStream ms = new MemoryStream(btBuffer, true);
        //        BinaryWriter bw = new BinaryWriter(ms);

        //        headerInfo.GetBytes(bw);
        //        EncodeInteger(bw, (int)notifyType);
        //        sendInfo.GetBytes(bw);

        //        bw.Close();
        //        ms.Close();

        //        socket.Send(btBuffer);

        //        _listSocketInfo.RemoveAt(0);
        //        _bSendFinished = true;
        //    }
        //    catch (Exception e)
        //    {
        //        SetError(ErrorType.Fail_SendSocket, "소켓송신 실패: " + e.Message);
        //        return false;
        //    }

        //    return true;
        //}

        public bool Send(Socket socket, NotifyType notifyType, BaseInfo sendInfo)
        {
            //SocketInfo socketInfo = new SocketInfo();
            //socketInfo.socket = socket;
            //socketInfo.notifyType = notifyType;
            //socketInfo.baseInfo = sendInfo;

            //_listSocketInfo.Add(socketInfo);

            try
            {
                if (socket.Connected == false)        // auto
                {
                    return(true);
                }

                int sendSize = 4 + sendInfo.GetSize();

                HeaderInfo headerInfo = new HeaderInfo();
                headerInfo._BodySize = sendSize;

                byte[] btBuffer = new byte[headerInfo.GetSize() + sendSize];

                MemoryStream ms = new MemoryStream(btBuffer, true);
                BinaryWriter bw = new BinaryWriter(ms);

                headerInfo.GetBytes(bw);
                EncodeInteger(bw, (int)notifyType);
                sendInfo.GetBytes(bw);

                bw.Close();
                ms.Close();

                //int nSendBuffersize = 0;

                //while (nSendBuffersize < btBuffer.Length)
                //{
                //    nSendBuffersize += socket.Send(btBuffer, nSendBuffersize, btBuffer.Length, SocketFlags.None);
                //}
                //if (notifyType == NotifyType.Reply_RoomDetail)
                //{
                //    System.Threading.Thread.Sleep(1000);
                //}

                SocketBuffer socketBuffer = FindBuffer(socket);

                socketBuffer._SendBuffers.Add(btBuffer);

                if (socketBuffer._SendBuffers.Count == 1)
                {
                    SendAsync(socket);
                }

                //System.DateTime dateTime1 = DateTime.Now;
                //byte[] tempBuffer = new byte[100000];
                //for (int i = 0; i < 10; i++)
                //{
                //    int nResult = socket.Send(tempBuffer);
                //    if (nResult != 10000)
                //    {
                //        SetError(ErrorType.Fail_SendSocket, "10000 fail");
                //    }
                //}

                //int nSum = 0;
                //DateTime dateTime2 = DateTime.Now;

                //double nSecond = (dateTime2 - dateTime1).TotalMilliseconds;
            }
            catch (Exception e)
            {
                SetError(ErrorType.Fail_SendSocket, "소켓송신 실패: " + e.Message);
                socket.Shutdown(SocketShutdown.Both);
                socket.Disconnect(true);
                socket.Close();
                _NotifyHandler(NotifyType.Notify_Socket, socket, null);
                return(false);
            }

            return(true);
        }