public static SocketMessageInfoType CheckMessageInfoType(string input) { SocketMessageInfoType _type = SocketMessageInfoType.NONE; string[] _names = Enum.GetNames(typeof(SocketMessageInfoType)); foreach (string name in _names) { if (input.IndexOf(string.Format("[MSG={0}]", name)) >= 0) { return(_type = (SocketMessageInfoType)Enum.Parse(typeof(SocketMessageInfoType), name)); } } return(_type); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="type"></param> /// <returns></returns> public void SendRequestAsync(SocketBaseRequest request, SocketMessageInfoType type = SocketMessageInfoType.REQUEST) { if (this.connected) { string _str = SerializeHelper.JsonSerializer(request); string message = string.Format("[length={0}][MSG={1}]{2}", _str.Length, type.ToString(), _str); byte[] sendBuffer = Encoding.Unicode.GetBytes(message); SendData(sendBuffer); } else { throw new SocketException((int)SocketError.NotConnected); } }
public static SocketRequestMessage GetActualString(string input, List <string> outputList = null) { if (outputList == null) { outputList = new List <string>(); } if (!string.IsNullOrEmpty(temp)) { input = temp + input; } string output = ""; string lenStr = @"(?<=^\[length=)(\d+)(?=\])"; string pattern = @"(\[MSG=)(JSON|XML|NONE|REQUEST)(?=\])"; int length = 0; SocketMessageInfoType _type = CheckMessageInfoType(input); if (Regex.IsMatch(input, lenStr)) { Match lenRex = Regex.Match(input, lenStr); length = Convert.ToInt32(lenRex.Groups[0].Value); if (Regex.IsMatch(input, pattern)) { Match m = Regex.Match(input, pattern); int startIndex = input.IndexOf(m.Groups[0].Value + "]") + m.Groups[0].Value.Length + 1; output = input.Substring(startIndex); if (output.Length == length) { outputList.Add(output); temp = ""; } else if (output.Length < length) { temp = input; } else if (output.Length > length) { output = output.Substring(0, length); outputList.Add(output); temp = ""; input = input.Substring(startIndex + length); GetActualString(input, outputList); } } } else { temp = input; } SocketRequestMessage message = new SocketRequestMessage() { MessageLength = length, MessageType = _type, Messages = outputList.ToArray() }; return(message); }
/// <summary> /// /// </summary> /// <param name="uid"></param> /// <param name="msg"></param> /// <param name="type"></param> public void SendMessageAsync(string uid, string msg, bool allUser = false, SocketMessageInfoType type = SocketMessageInfoType.NONE) { if ((!allUser && string.IsNullOrEmpty(uid)) || string.IsNullOrEmpty(msg)) { return; } if (allUser) { if (this.m_readWritePool.m_busypool.Count == 0) { LogOutEvent(null, SocketMessageType.Error, string.Format("No client online")); } foreach (string id in this.m_readWritePool.m_busypool.Keys) { SendMessageAsync(id, msg, false, type); } } else { SocketAsyncEventArgsWithIdDuplex _socket = m_readWritePool.FindByUID(uid); if (_socket == null) { OnSended(uid, SocketMessageType.UseOffline.ToString()); } else { string _msgTmpl = @"[length={0}][MSG={1}]{2}"; SocketAsyncEventArgsImpl _e = _socket.SendSAEA; if (_e.SocketError == SocketError.Success) { int _i = 0; try { string _msg = string.Format(_msgTmpl, msg.Length, type.ToString(), msg); byte[] _sendBuffer = Encoding.Unicode.GetBytes(_msg); _e.SetBuffer(_sendBuffer, 0, _sendBuffer.Length); bool _willRaiseEvent = (_e.UserToken as Socket).SendAsync(_e); if (!_willRaiseEvent) { this.ProcessSend(_e); } } catch (Exception ee) { if (_i <= 5) { _i++; Thread.Sleep(10); SendMessageAsync(uid, msg, allUser, type); } else { OnSended(uid, ee.ToString()); } } } else { OnSended(uid, SocketMessageType.Failed.ToString()); this.CloseClientSocket(_e.UID, false); } } } }